﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class shoppingmall_bll : base_dll
    {
        public List<Wjtour.Domain.shoppingmall> GetList(int top)
        {
            string sql = string.Format("select top {0} * from shoppingmall order by isrecommend desc, id desc", top);
            return GetAll<Wjtour.Domain.shoppingmall>(sql).ToList();
        }

        public PaginationData<Wjtour.Domain.shoppingmall> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and title like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from shoppingmall 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.shoppingmall> data = GetPaginationData<Wjtour.Domain.shoppingmall>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.shoppingmall Load(int id)
        {
            string sql = "select * from shoppingmall where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.shoppingmall>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.shoppingmall entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE shoppingmall SET 
                title = @title,
                logo = @logo,
                introduce = @introduce,
                addtime = @addtime,
                isrecommend = @isrecommend
                WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO shoppingmall( 
title,logo,introduce,addtime,isrecommend)VALUES(
@title,@logo,@introduce,@addtime,@isrecommend)";
            }
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@logo", DbType.String, entity.logo);
            DbAccess.AddInParameter(cmd, "@introduce", DbType.String, entity.introduce);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@isrecommend", DbType.Int32, entity.isrecommend);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from shoppingmall where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
